﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Reflection;
using System.Text;

namespace WFL.Data.SqlClient
{
    public class SqlTransactionProxy : ITransactionProxy
    {
        internal SqlTransaction Transaction { get; private set; }
        internal SqlConnectionProxy ConnectionProxy { get; set; }
        internal MethodBase CreatingMethod { get; set; }

        public SqlTransactionProxy(SqlTransaction transaction)
        {
            Transaction = transaction;
        }


        #region ITransactionProxy 成员

        public void Commit()
        {
            MethodBase callingMethod = new StackFrame(1).GetMethod();
            if (callingMethod == CreatingMethod)
            {
                ConnectionProxy.TransactionProxy = null;
                Transaction.Commit();
            }
        }

        public void Rollback()
        {
            MethodBase callingMethod = new StackFrame(1).GetMethod();
            if (callingMethod == CreatingMethod)
            {
                ConnectionProxy.TransactionProxy = null;
                Transaction.Rollback();
            }
        }

        #endregion
    }
}
